package cn.edu.nsmc.modules.sys.service.impl;

import cn.edu.nsmc.common.utils.PageUtils;
import cn.edu.nsmc.common.utils.Query;
import cn.edu.nsmc.common.utils.R;
import cn.edu.nsmc.modules.sys.dao.SysWorksheetAuditNodeDao;
import cn.edu.nsmc.modules.sys.entity.SysUserEntity;
import cn.edu.nsmc.modules.sys.entity.SysWorksheetAuditNode;
import cn.edu.nsmc.modules.sys.entity.data.TicketLoggingData;
import cn.edu.nsmc.modules.sys.entity.data.TicketStatusData;
import cn.edu.nsmc.modules.sys.service.SysUserService;
import cn.edu.nsmc.modules.sys.service.SysWorksheetAuditNodeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("sysWorksheetAuditNodeService")
public class SysWorksheetAuditNodeServiceImpl extends ServiceImpl<SysWorksheetAuditNodeDao, SysWorksheetAuditNode> implements SysWorksheetAuditNodeService {
    SysUserService sysUserService;

    @Autowired
    SysWorksheetAuditNodeDao sysWorksheetAuditNodeDao;

    @Autowired
    public SysWorksheetAuditNodeServiceImpl(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysWorksheetAuditNode> page = this.page(
                new Query<SysWorksheetAuditNode>().getPage(params),
                new QueryWrapper<SysWorksheetAuditNode>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SysWorksheetAuditNode> queryByWorksheetId(Long worksheetId) {
        LambdaQueryWrapper<SysWorksheetAuditNode> queryWrapper = new LambdaQueryWrapper<>();
        List<SysWorksheetAuditNode> list = this.list(queryWrapper.eq(SysWorksheetAuditNode::getWorksheetId, worksheetId));
        for (SysWorksheetAuditNode item : list) {
            Long auditor = item.getAuditor();
            try{
                SysUserEntity byId = sysUserService.getById(auditor);
                item.setApprovedName(byId.getUsername());
            } catch (NullPointerException ex){
                log.error("操作人不存在 id : "+auditor);
                item.setApprovedName("已注销");
            }
        }
        return list;
    }

    @Override
    public R getInformation() throws ParseException {
        List<TicketStatusData> ticketStatusData = sysWorksheetAuditNodeDao.getInformation();
        TicketLoggingData []  arr = new TicketLoggingData[8] ;

        int day=0;
        java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd");
        String now3 = format.format(System.currentTimeMillis());
        Date bdatea = format.parse(now3);
        for(int i = 0; i <= 7; i++){
            arr[i] = new TicketLoggingData();
            arr[i].setFinishNumber(0);
            arr[i].setRefuseNumber(0);
            arr[i].setTime(format.format(bdatea.getTime() - i*24 * 60 * 60 * 1000));
        }
        for (TicketStatusData l : ticketStatusData) {
            String p = format.format(l.getTime());
            Date edatea = format.parse(p);
            day=(int)(bdatea.getTime()-edatea.getTime())/(24*60*60*1000);
            if (l.getAuditStatus() == -1){
                arr[day].setRefuseNumber(arr[day].getRefuseNumber() + 1);
            }else{
                arr[day].setFinishNumber(arr[day].getFinishNumber() + 1);
            }
        }

        return R.success("成功查询",arr);
    }

}
